﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace GMusicBox.Core
{
    public class Settings
    {
        string fn;
        List<string> propertys = new List<string>();
        List<string> values = new List<string>();

        public Settings() { }

        public Settings(string FileName)
        {
            fn = FileName;
            ReLoadSettings();
        }

        public string this[string property]
        {
            get
            {
                for (int i = 0; i < propertys.Count; i++)
                    if (propertys[i] == property) return values[i];
                return "";
            }
            set
            {
                int i = 0;
                for (i = 0; i < propertys.Count; i++)
                {
                    if (propertys[i] == property)
                    {
                        values[i] = value;
                        break;
                    }
                }
                if (i == propertys.Count)
                    this.Write(property, value);
            }
        }

        public void Clear()
        {
            propertys.Clear();
            values.Clear();
        }

        public void ReLoadSettings()
        {
            propertys.Clear();
            values.Clear();
            LoadSettings(fn);
        }

        public void LoadSettings(string filename)
        {
            if (!File.Exists(filename)) return;
            StreamReader sr = new StreamReader(filename, Encoding.UTF8);
            string s = sr.ReadToEnd(); sr.Close();
            string[] configs = s.Trim().Split('=', '\n');
            for (int i = 5; i < configs.Length - 1; i++)
            {
                configs[i] = configs[i].Replace("\t", "").Trim();
                int pe = configs[i].IndexOf(">");
                propertys.Add(configs[i].Trim().Substring(1, pe - 1));

                int pe1 = configs[i].LastIndexOf("</");
                values.Add(configs[i].Substring(pe + 1, pe1 - pe - 1));
            }
        }

        public bool HasProperty(string name)
        {
            for (int i = 0; i < propertys.Count; i++)
                if (propertys[i] == name) return true;
            return false;
        }

        public void WriteIfNotExist(string name, string value)
        {
            if (!HasProperty(name)) Write(name, value);
        }

        public void Write(string pty, string value)
        {
            propertys.Add(pty);
            values.Add(value);
        }

        public void WriteAllSettings()
        {
            WriteAllSettings(fn);
        }

        public void WriteAllSettings(string filename)
        {
            string s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\r\n<" + Path.GetFileNameWithoutExtension(filename) + ">\r\n";
            for (int i = 0; i < Propertys.Count; i++)
            {
                s += "\t<" + propertys[i] + ">";
                s += values[i] + "</" + propertys[i] + ">";
                s += "\r\n";
            }
            s += "</" + Path.GetFileNameWithoutExtension(filename) + ">";

            Helper.CreateDirectory(filename.Substring(0, filename.LastIndexOf("\\")));
            File.WriteAllText(filename, s);
        }

        public void Update(string prt, string value)
        {
            this[prt] = value;
        }

        public static string ReadSetting(string filename, string property)
        {
            Settings s = new Settings(filename);
            return s[property];
        }

        public static bool ConvertToBool(string s)
        {
            if (s == "0") return false;
            else return true;
        }

        public List<string> Propertys
        {
            get { return propertys; }
            set { propertys = value; }
        }

        public List<string> Values
        {
            get { return values; }
            set { values = value; }
        }

        public static string SettingSavePath
        {
            get
            {
                if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\GMusicBox" + "\\true"))
                    return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\GMusicBox";
                else return System.Windows.Forms.Application.StartupPath;
            }
        }
    }
}
